#!/usr/bin/perl -w
#
# $Id: DrawingArea.pm,v 1.3 2003/07/17 19:32:24 solovam Exp $
#
# This file is a part of gkismet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# DrawingArea class
#
package DrawingArea;

use Gnome;
use Misc;
use strict;

#
# Constructor
#
sub new
{
	my $type = shift;
	my $self = {};
	bless $self, $type;
	$self->{'width'} = shift;
	$self->{'height'} = shift;
	$self->{'draw'} = shift;

	my $gtkDrawingArea = new Gtk::DrawingArea();
	$self->{'gtkDrawingArea'} = $gtkDrawingArea;
	$gtkDrawingArea->size($self->{'width'}, $self->{'height'});
	$gtkDrawingArea->signal_connect('configure_event', sub{$self->configureHandler(@_)});
	$gtkDrawingArea->signal_connect('expose_event', sub{$self->exposeHandler(@_)});

	return $self;
}

#
# Handle configure event from drawing area
#
sub configureHandler
{
	my $self = shift;
	my $gtkDrawingArea = shift;
	my $event = shift;
	my $pixmap = new Gtk::Gdk::Pixmap($gtkDrawingArea->window(), $gtkDrawingArea->allocation()->[2], $gtkDrawingArea->allocation()->[3], -1);
	$self->{'pixmap'} = $pixmap;
	if(!$self->{'gcBuilt'})
        {
		$self->buildGC();
        }
	&{$self->{'draw'}}();
	return $true;
}

#
# Handle expose event from drawing area
#
sub exposeHandler
{
	my $self = shift;
	my $gtkDrawingArea = shift;
	my $event = shift;
	$gtkDrawingArea->window()->draw_pixmap($gtkDrawingArea->style()->fg_gc('normal'), $self->{'pixmap'},
		$event->{'area'}[0], $event->{'area'}[1], $event->{'area'}[0], $event->{'area'}[1], $event->{'area'}[2], $event->{'area'}[3]);
	return $false;
}

#
# Get string height
#
sub stringHeight
{
	my $self = shift;
	my $window;
	if(!ref($self))
	{
		$window = shift;
	}
	else
	{
		$window = $self->{'gtkDrawingArea'};
	}
	my $string = shift;

	my $stringHeight = ($window->style()->font()->string_extents($string))[3] + ($window->style()->font()->string_extents($string))[4];

	return $stringHeight;
}

#
# Get string width
#
sub stringWidth
{
	my $self = shift;
	my $window;
	if(!ref($self))
	{
		$window = shift;
	}
	else
	{
		$window = $self->{'gtkDrawingArea'};
	}
	my $string = shift;

	my $stringWidth = $window->style()->font()->string_measure($string);

	return $stringWidth;
}

#
# Build some GCs
#
sub buildGC
{
	my $self =shift;
	my $gtkDrawingArea = $self->{'gtkDrawingArea'};
	my $redGC = new Gtk::Gdk::GC($gtkDrawingArea->window());
	my $blueGC = new Gtk::Gdk::GC($gtkDrawingArea->window());
	my $greenGC = new Gtk::Gdk::GC($gtkDrawingArea->window());
	my $redColor = $gtkDrawingArea->window()->get_colormap->color_alloc(Gtk::Gdk::Color->parse_color('red'));
	my $greenColor = $gtkDrawingArea->window()->get_colormap->color_alloc(Gtk::Gdk::Color->parse_color('green'));
	my $blueColor = $gtkDrawingArea->window->get_colormap->color_alloc(Gtk::Gdk::Color->parse_color('blue'));
	$redGC->set_foreground($redColor);
	$greenGC->set_foreground($greenColor);
	$blueGC->set_foreground($blueColor);
	$self->{'redGC'} = $redGC;
	$self->{'greenGC'} = $greenGC;
	$self->{'blueGC'} = $blueGC;
	$self->{'gcBuilt'} = $true;
}

#
# Return widget
#
sub getWidget
{
	my $self = shift;
	return $self->{'gtkDrawingArea'};
}

#
# Return drawing area
#
sub gtkDrawingArea
{
	my $self = shift;
	return $self->{'gtkDrawingArea'};
}

#
# Return pixmap
#
sub pixmap
{
	my $self = shift;
	return $self->{'pixmap'};
}

1;
